﻿namespace OleDbDAL.Procurement
{
    using DBUtility;
    using IDal.Procurement;
    using Model.Procurement;
    using Model.Systems;
    using System;
    using System.Data;
    using System.Text;
    using WinFormSession;

    public class ProcurementCommBuyApp : IProcurementCommBuy
    {
        private string dbname = "Systems";

        public void Add(ProcurementCommBuyEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into T_ProcurementCommBuy(");
            builder.Append("ProcurementId,CommId,CommName,LotNum,DllId,ParId,Amount,AmoutUnit,AmoutMoney,Rate,RateAllowance,Features,IsAccep,IsRejection,IsReturn,ReturnAmount,Remark");
            builder.Append(")");
            builder.Append(" values (");
            builder.Append("" + model.ProcurementId + ",");
            builder.Append("" + model.CommId + ",");
            builder.Append("'" + model.CommName + "',");
            builder.Append("'" + model.LotNum + "',");
            builder.Append("" + model.DllId + ",");
            builder.Append("" + model.ParId + ",");
            builder.Append("" + model.Amount + ",");
            builder.Append("" + model.AmoutUnit + ",");
            builder.Append("" + model.AmoutMoney + ",");
            builder.Append("" + model.Rate + ",");
            builder.Append("" + model.RateAllowance + ",");
            builder.Append("'" + model.Features + "',");
            builder.Append("" + model.IsAccep + ",");
            builder.Append("" + model.IsRejection + ",");
            builder.Append("" + model.IsReturn + ",");
            builder.Append("" + model.ReturnAmount + ",");
            builder.Append("'" + model.Remark + "'");
            builder.Append(")");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public void Delete(int CommBuyId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from T_ProcurementCommBuy ");
            builder.Append(" where CommBuyId=" + CommBuyId);
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public DataTable GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_ProcurementCommBuy ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by CommBuyId ");
            return DbHelperOledb.Query(this.dbname, builder.ToString());
        }

        public int GetMaxId(string swhere)
        {
            return DbHelperOledb.GetMaxID(this.dbname, "CommBuyId", "T_ProcurementCommBuy", swhere);
        }

        public ProcurementCommBuyEnt GetModel(int CommBuyId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select CommBuyId,ProcurementId,CommId,CommName,LotNum,DllId,ParId,Amount,AmoutUnit,AmoutMoney,Rate,RateAllowance,Features,IsAccep,IsRejection,IsReturn,ReturnAmount,Remark from T_ProcurementCommBuy ");
            builder.Append(" where CommBuyId=" + CommBuyId);
            ProcurementCommBuyEnt ent = new ProcurementCommBuyEnt();
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            ent.CommBuyId = CommBuyId;
            if (table.Rows.Count > 0)
            {
                if (table.Rows[0]["ProcurementId"].ToString() != "")
                {
                    ent.ProcurementId = int.Parse(table.Rows[0]["ProcurementId"].ToString());
                }
                if (table.Rows[0]["CommId"].ToString() != "")
                {
                    ent.CommId = int.Parse(table.Rows[0]["CommId"].ToString());
                }
                ent.CommName = table.Rows[0]["CommName"].ToString();
                ent.LotNum = table.Rows[0]["LotNum"].ToString();
                if (table.Rows[0]["DllId"].ToString() != "")
                {
                    ent.DllId = int.Parse(table.Rows[0]["DllId"].ToString());
                }
                if (table.Rows[0]["ParId"].ToString() != "")
                {
                    ent.ParId = int.Parse(table.Rows[0]["ParId"].ToString());
                }
                if (table.Rows[0]["Amount"].ToString() != "")
                {
                    ent.Amount = decimal.Parse(table.Rows[0]["Amount"].ToString());
                }
                if (table.Rows[0]["AmoutUnit"].ToString() != "")
                {
                    ent.AmoutUnit = int.Parse(table.Rows[0]["AmoutUnit"].ToString());
                }
                if (table.Rows[0]["AmoutMoney"].ToString() != "")
                {
                    ent.AmoutMoney = decimal.Parse(table.Rows[0]["AmoutMoney"].ToString());
                }
                if (table.Rows[0]["Rate"].ToString() != "")
                {
                    ent.Rate = decimal.Parse(table.Rows[0]["Rate"].ToString());
                }
                if (table.Rows[0]["RateAllowance"].ToString() != "")
                {
                    ent.RateAllowance = decimal.Parse(table.Rows[0]["RateAllowance"].ToString());
                }
                ent.Features = table.Rows[0]["Features"].ToString();
                if (table.Rows[0]["IsAccep"].ToString() != "")
                {
                    ent.IsAccep = int.Parse(table.Rows[0]["IsAccep"].ToString());
                }
                if (table.Rows[0]["IsRejection"].ToString() != "")
                {
                    ent.IsRejection = int.Parse(table.Rows[0]["IsRejection"].ToString());
                }
                if (table.Rows[0]["IsReturn"].ToString() != "")
                {
                    ent.IsReturn = int.Parse(table.Rows[0]["IsReturn"].ToString());
                }
                if (table.Rows[0]["ReturnAmount"].ToString() != "")
                {
                    ent.ReturnAmount = decimal.Parse(table.Rows[0]["ReturnAmount"].ToString());
                }
                ent.Remark = table.Rows[0]["Remark"].ToString();
                return ent;
            }
            return null;
        }

        public DataTable GetPageList(int PageSize, int PageIndex, string strWhere)
        {
            string orderString = "";
            int pageCount = 0;
            int recordCount = 0;
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_ProcurementCommBuy ");
            return DbHelperOledb.ExecutePager(this.dbname, PageIndex, PageSize, "", builder.ToString(), strWhere, orderString, out pageCount, out recordCount, "CommBuyId");
        }

        public DataTable SelectData(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            UserEnt ent = (UserEnt) WinformSession.GetSession()["user"];
            builder.Append("select CommBuyId,IDDocuments,LotNum,CommName,Amount,AmoutMoney,Features,Amount*AmoutMoney as AllMoney,T_Procurement.CreateTime,");
            builder.Append("(select SupplierName from T_Supplier where T_Procurement.Supplierid=T_Supplier.Supplierid) as SupplierName,");
            builder.Append("(select DlName from T_Digitallibrary where T_Digitallibrary.DlId=T_ProcurementCommBuy.DllId) as DlName,");
            builder.Append("(select parName from T_Parameter where T_ProcurementCommBuy.AmoutUnit=T_Parameter.ParId) as AmoutUnit,");
            builder.Append("(select UserName from T_User where T_User.UserId=T_Procurement.ProcuMan) as ProcuMan");
            builder.Append(" from T_Procurement,T_ProcurementCommBuy");
            builder.Append(" where T_Procurement.ProcurementId=T_ProcurementCommBuy.ProcurementId and T_Procurement.IsBlackOut=0 and T_Procurement.ChainNum=" + ent.ChainNum);
            builder.Append(strWhere);
            builder.Append(" order by CommBuyId desc");
            return DbHelperOledb.Query(this.dbname, builder.ToString());
        }

        public DataTable SelectData(int PageSize, int PageIndex, string strWhere)
        {
            int pageCount = 0;
            int recordCount = 0;
            StringBuilder builder = new StringBuilder();
            UserEnt ent = (UserEnt) WinformSession.GetSession()["user"];
            builder.Append("select CommBuyId,IDDocuments,LotNum,CommName,Amount,AmoutMoney,Features,Amount*AmoutMoney as AllMoney,T_Procurement.CreateTime,");
            builder.Append("(select SupplierName from T_Supplier where T_Procurement.Supplierid=T_Supplier.Supplierid) as SupplierName,");
            builder.Append("(select DlName from T_Digitallibrary where T_Digitallibrary.DlId=T_ProcurementCommBuy.DllId) as DlName,");
            builder.Append("(select parName from T_Parameter where T_ProcurementCommBuy.AmoutUnit=T_Parameter.ParId) as AmoutUnit,");
            builder.Append("(select UserName from T_User where T_User.UserId=T_Procurement.ProcuMan) as ProcuMan");
            builder.Append(" from T_Procurement,T_ProcurementCommBuy");
            builder.Append(" where T_Procurement.ProcurementId=T_ProcurementCommBuy.ProcurementId and T_Procurement.IsBlackOut=0 and T_Procurement.ChainNum=" + ent.ChainNum);
            builder.Append(strWhere);
            builder.Append(" order by CommBuyId desc");
            return DbHelperOledb.ExecutePager(this.dbname, PageIndex, PageSize, "", builder.ToString(), "", "$%", out pageCount, out recordCount, "CommBuyId");
        }

        public int SelectDataCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            UserEnt ent = (UserEnt) WinformSession.GetSession()["user"];
            builder.Append("select count(*) ");
            builder.Append(" from T_Procurement,T_ProcurementCommBuy");
            builder.Append(" where T_Procurement.ProcurementId=T_ProcurementCommBuy.ProcurementId and T_Procurement.IsBlackOut=0 and T_Procurement.ChainNum=" + ent.ChainNum);
            builder.Append(strWhere);
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            if (table.Rows.Count > 0)
            {
                return int.Parse(table.Rows[0][0].ToString());
            }
            return 0;
        }

        public void Update(ProcurementCommBuyEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update T_ProcurementCommBuy set ");
            builder.Append("ProcurementId=" + model.ProcurementId + ",");
            builder.Append("CommId=" + model.CommId + ",");
            builder.Append("CommName='" + model.CommName + "',");
            builder.Append("LotNum='" + model.LotNum + "',");
            builder.Append("DllId=" + model.DllId + ",");
            builder.Append("ParId=" + model.ParId + ",");
            builder.Append("Amount=" + model.Amount + ",");
            builder.Append("AmoutUnit=" + model.AmoutUnit + ",");
            builder.Append("AmoutMoney=" + model.AmoutMoney + ",");
            builder.Append("Rate=" + model.Rate + ",");
            builder.Append("RateAllowance=" + model.RateAllowance + ",");
            builder.Append("Features='" + model.Features + "',");
            builder.Append("IsAccep=" + model.IsAccep + ",");
            builder.Append("IsRejection=" + model.IsRejection + ",");
            builder.Append("IsReturn=" + model.IsReturn + ",");
            builder.Append("ReturnAmount=" + model.ReturnAmount + ",");
            builder.Append("Remark='" + model.Remark + "'");
            builder.Append(" where CommBuyId=" + model.CommBuyId + "");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }
    }
}

